package com.feihong.txgw.tx_model;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.feihong.txgw.inter.contract.kaika.ISubscribeKaikaContract;
import com.feihong.txgw.model.bean.kaika.KaikaNewPackageEntity;
import com.feihong.txgw.model.PRODUCT_TYPE;
import com.feihong.txgw.model.bean.deliveryAdr.DeliveryAdrEntity;
import com.feihong.txgw.model.bean.kaika.YuyueViewShowEnity;
import com.feihong.txgw.ui.activity.user.MyDeliveryAdrActivity;
import com.feihong.txgw.utils.PreferenceTools;
import com.feihong.txgw.utils.adress.StringEmpty;
import com.feihong.txgw.utils.helper.RxHelper;
import com.feihong.txgw.utils.kaika.KaikaIntentType;
import com.feihong.txgw.utils.manage.RetrofitManager;

import io.reactivex.Observable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName SubscribeKaikaMainModel
 * @package com.feihong.txgw.tx_model
 * @data 2018/5/7 12:16
 * @Describe
 * @Change
 */
public class SubscribeKaikaMainModel implements ISubscribeKaikaContract.ISubscribeKaikaMainModel {

    private KaikaNewPackageEntity mHomePackageEntity;
    private YuyueViewShowEnity mYuyueViewShowEnity;
    private DeliveryAdrEntity mResultDeliveryAdrEntity;
    public static SubscribeKaikaMainModel create(){
        return new SubscribeKaikaMainModel();
    }

    @Override
    public Observable<String> submitOrder(String specID, String phoneNum, String adrID, String deliveryType) {
        return RetrofitManager.getInstance().submitSubscribeKaikaOrder(specID,phoneNum,adrID,deliveryType).compose(RxHelper.<String>rxSchedulerHelper());
    }

    @Override
    public Observable<YuyueViewShowEnity> parseBundleAndGetAdr(Bundle bundle) {
        mYuyueViewShowEnity = new YuyueViewShowEnity();
        parseBundle(bundle);
        getDefAdr();
        return Observable.just(mYuyueViewShowEnity);
    }

    /**
     * 获取选择的地址
     * @return
     */
    @Override
    public Observable<YuyueViewShowEnity> getNewAdr() {
        String mAdress;
        mYuyueViewShowEnity = new YuyueViewShowEnity();
        if (TextUtils.isEmpty(PreferenceTools.getString(MyDeliveryAdrActivity.SELECT_ADR_KEY, ""))) {
            if (!TextUtils.isEmpty(PreferenceTools.getString(MyDeliveryAdrActivity.DEFAULT_ADR_KEY, ""))) {
                mResultDeliveryAdrEntity = JSON.parseObject(PreferenceTools.getString(MyDeliveryAdrActivity.DEFAULT_ADR_KEY, null), DeliveryAdrEntity.class);
                // 兼容1.1.0版本 如果是老地址的话 会因为新接口的变动缺少返回参数   等同于没有地址信息
                if (mResultDeliveryAdrEntity != null && !StringEmpty.isEmpty(mResultDeliveryAdrEntity.getAddress()) && !StringEmpty.isEmpty(mResultDeliveryAdrEntity.getLocation_info())) {
                    mAdress = (StringEmpty.isEmpty(mResultDeliveryAdrEntity.getAddress()) ? "" : mResultDeliveryAdrEntity.getAddress())
                            + (StringEmpty.isEmpty(mResultDeliveryAdrEntity.getHouse_num()) ? "" : mResultDeliveryAdrEntity.getHouse_num());
                    mYuyueViewShowEnity.setAdress(mAdress);
                    mYuyueViewShowEnity.setAdressID(mResultDeliveryAdrEntity.getAddr_id());
                } else {
                    mResultDeliveryAdrEntity = new DeliveryAdrEntity();
                    mYuyueViewShowEnity.setAdress(null);
                    mYuyueViewShowEnity.setAdressID(null);
                }
            }
        } else {
            String selectAdrStr = PreferenceTools.getString(MyDeliveryAdrActivity.SELECT_ADR_KEY, null);
            mResultDeliveryAdrEntity = JSON.parseObject(selectAdrStr, DeliveryAdrEntity.class);
            // 以下判断待删除
            if (mResultDeliveryAdrEntity != null && !StringEmpty.isEmpty(mResultDeliveryAdrEntity.getAddress()) && !StringEmpty.isEmpty(mResultDeliveryAdrEntity.getLocation_info())) {
                mAdress = (StringEmpty.isEmpty(mResultDeliveryAdrEntity.getAddress()) ? "" : mResultDeliveryAdrEntity.getAddress())
                        + (StringEmpty.isEmpty(mResultDeliveryAdrEntity.getHouse_num()) ? "" : mResultDeliveryAdrEntity.getHouse_num());
                mYuyueViewShowEnity.setAdress(mAdress);
                mYuyueViewShowEnity.setAdressID(mResultDeliveryAdrEntity.getAddr_id());
            } else {
                mResultDeliveryAdrEntity = new DeliveryAdrEntity();
                mYuyueViewShowEnity.setAdress(null);
                mYuyueViewShowEnity.setAdressID(null);
            }
        }

        return Observable.just(mYuyueViewShowEnity);
    }

    /**
     * 获取默认地址
     */
    private void getDefAdr() {
        PreferenceTools.putString(MyDeliveryAdrActivity.SELECT_ADR_KEY, PreferenceTools.getString(MyDeliveryAdrActivity.DEFAULT_ADR_KEY, ""));
        if (TextUtils.isEmpty(PreferenceTools.getString(MyDeliveryAdrActivity.DEFAULT_ADR_KEY, ""))) {
            mResultDeliveryAdrEntity = new DeliveryAdrEntity();
        } else {
            mResultDeliveryAdrEntity = JSON.parseObject(PreferenceTools.getString(MyDeliveryAdrActivity.DEFAULT_ADR_KEY, null), DeliveryAdrEntity.class);
            if (mResultDeliveryAdrEntity != null && !StringEmpty.isEmpty(mResultDeliveryAdrEntity.getAddress()) && !StringEmpty.isEmpty(mResultDeliveryAdrEntity.getLocation_info())) {
                String mAdress = (StringEmpty.isEmpty(mResultDeliveryAdrEntity.getAddress()) ? "" : mResultDeliveryAdrEntity.getAddress())
                        + (StringEmpty.isEmpty(mResultDeliveryAdrEntity.getHouse_num()) ? "" : mResultDeliveryAdrEntity.getHouse_num());
                mYuyueViewShowEnity.setAdress(mAdress);
                mYuyueViewShowEnity.setAdressID(mResultDeliveryAdrEntity.getAddr_id());
            } else {
                mResultDeliveryAdrEntity = new DeliveryAdrEntity();
            }
        }
    }

    /**
     * 获取套餐信息or号码信息
     * @param bundle
     */
    private void parseBundle(Bundle bundle) {
        if (bundle != null) {
            // 从首页跳转而来
            mHomePackageEntity = bundle.getParcelable(KaikaIntentType.HOME_INTENT_PACAKGE_INFO_KEY);
            if (mHomePackageEntity != null) {
                if (mHomePackageEntity.getType().equals(PRODUCT_TYPE.TAG_PHONE_TARIFF)) {
                    // 套餐类型
                    mYuyueViewShowEnity.setPackageName(mHomePackageEntity.getSpec_name());
                    mYuyueViewShowEnity.setPackageIcpID(mHomePackageEntity.getSpec_id());
                } else {
                    //  号码类型
                    mYuyueViewShowEnity.setPhoneNum(mHomePackageEntity.getNumber());
                }
            }
        }
    }
}
